/*
 *  Copyright (C) 2010 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.balisunrise.nbpam;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import javax.swing.Icon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import org.balisunrise.application.Application;
import org.balisunrise.application.MenuFolder;
import org.balisunrise.application.Module;
import org.balisunrise.common.MenuAction;
import org.balisunrise.nbpam.components.ToolbarModuleButton;
import org.openide.util.ImageUtilities;
import org.openide.util.actions.Presenter;

public final class LoaderAction implements Presenter.Toolbar, Presenter.Menu,
        ActionListener {

    JMenu menu;
    JPanel modulesPanel;
    int oldModules = 0,
            oldp = 0;

    @Override
    public void actionPerformed(ActionEvent e) {
        // empty
    }

    @Override
    public Component getToolbarPresenter() {

        Application app = Application.get();

        int cols = 1;
        if(app.getModules() != null && app.getModules().length > 0){
            cols = app.getModules().length;
            if (cols != oldModules)
                oldModules = cols;
            else
                return modulesPanel;
        }

        modulesPanel = new JPanel(
                new GridLayout(1, cols));

        for (Module module : app.getModules()) {
            ToolbarModuleButton b = new ToolbarModuleButton(module);
            modulesPanel.add(b);
        }

        System.out.println("T O O L B A R");

        return modulesPanel;
    }

    @Override
    public JMenuItem getMenuPresenter() {

        if(menu == null){
            menu = new JMenu("Módulos");
        }

        Module[] modules = Application.get().getModules();
        if(modules != null && modules.length > 0){
            if(modules.length != oldp)
                oldp = modules.length;
            else
                return menu;
        }

        menu.removeAll();
        for (Module module : modules) {
            JMenu m = new JMenu(module.getDescription());
            menu.add(m);
            buildMenus(m, module);
        }

        System.out.println("M E N U S");

        return menu;
    }

    private void buildMenus(JMenu menu, MenuFolder folder){
        for (MenuFolder mf : folder.getSubFolders()) {
            JMenu subMenu = new JMenu(mf.getName());
            menu.add(subMenu);
            buildMenus(subMenu, mf);
        }
        for (MenuAction ma : folder.getActions()) {
            String name = ma.getDescription();
            Icon icon = null;
            try{
                if(ma.getIcon() != null && ma.getIcon().length() > 0)
                    icon = ImageUtilities.image2Icon(
                            ImageUtilities.loadImage(ma.getIcon()));
            }catch(Exception ex){
                Application.get().getLogger().log(Level.SEVERE,
                        "Falha ao carregar Menus.", ex);
            }
            JMenuItem mi = new JMenuItem(name, icon);
            mi.addActionListener(new MenuActionListener(ma));
            menu.add(mi);
        }
    }

    private class MenuActionListener implements ActionListener{

        MenuAction action;

        public MenuActionListener(MenuAction action) {
            this.action = action;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            action.getAction().perform(null);
        }

    }
}
